//
//  KURLLoader.m
//  Ghac 4S
//
//  Created by apple on 11-8-26.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "KURLLoader.h"
#import "KURLVariable.h"
#import "KURLRequest.h"

@implementation KURLLoader
@synthesize data = m_data;

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
        m_data = [[NSMutableData alloc] init];
        m_eventList = [[NSMutableDictionary alloc] init];
    }
    
    return self;
}
-(id)initWithDelegate:(id)delegate
{
    self = [super init];
    if (self) {
        // Initialization code here.
        m_data = [[NSMutableData alloc] init];
        m_eventList = [[NSMutableDictionary alloc] init];
        if (m_delegate != Nil)
        {
            [m_delegate release];
            m_delegate = Nil;
        }
        m_delegate = [delegate retain];
    }
    return self;
}
-(void)load:(KURLRequest *)request
{
    if (request.variable != Nil)
    {
        [request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
        [request setHTTPBody:[[request.variable.variablesString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] dataUsingEncoding:NSUTF8StringEncoding]];
    }
    
    NSURLConnection*    con = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [con start];
}
-(void)addEventListener:(NSString *)type sector:(SEL)function
{
    NSMutableArray* functionList = Nil;
    NSValue* value = [NSValue valueWithPointer:function];
    if([m_eventList objectForKey:type] == Nil)
    {
        functionList = [[NSMutableArray alloc] initWithObjects:value, nil];
        [m_eventList setObject:functionList forKey:type];
    }
    else
    {
        functionList = [m_eventList objectForKey:type];
        [functionList addObject:value];
    }
    
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [m_data appendData:data];
    NSMutableArray* funcitonList = [m_eventList objectForKey:KLOADER_PROCESS];
    if (funcitonList != Nil && m_delegate != Nil)
    {
        for (int i = 0; i < funcitonList.count; i++)
        {
            SEL function = (SEL)[[funcitonList objectAtIndex:i] pointerValue];
            [m_delegate performSelector:function withObject:self];
        }
    }
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSMutableArray* funcitonList = [m_eventList objectForKey:KLOADER_COMPLETE];
    if (funcitonList != Nil && m_delegate != Nil)
    {
        for (int i = 0; i < funcitonList.count; i++)
        {
            SEL function = (SEL)[[funcitonList objectAtIndex:i] pointerValue];
            [m_delegate performSelector:function withObject:self];
        }
    }
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSMutableArray* funcitonList = [m_eventList objectForKey:KLOADER_ERROR];
    if (funcitonList != Nil && m_delegate != Nil)
    {
        for (int i = 0; i < funcitonList.count; i++)
        {
            SEL function = (SEL)[[funcitonList objectAtIndex:i] pointerValue];
            [m_delegate performSelector:function withObject:self];
        }
    }
}
-(void)dealloc
{
    self.data = Nil;
    [m_delegate release];
    m_delegate = nil;
    [super dealloc];
}

@end
